package com.example.demo.api.db.dao;

import org.apache.phoenix.queryserver.client.Driver;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

@Repository
public class TripDao {
    public ArrayList<HashMap> searchUserTrip(String tel,String startDate,String endDate) throws Exception{
        DriverManager.registerDriver(new Driver());
        Connection con = DriverManager.getConnection("jdbc:phoenix:thin:url=http://192.168.1.107:8765;serialization=PROTOBUF");
        String sql = "select * from test.trip where tel=? and TO_CHAR(create_time,'yyyy-MM-dd') between ? and ?";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setObject(1,tel);
        pst.setObject(2,startDate);
        pst.setObject(3,endDate);
        ResultSet set = pst.executeQuery();
        ArrayList list = new ArrayList();
        while(set.next()){
            HashMap map = new HashMap();
            map.put("id", set.getInt("id"));
            map.put("tel", set.getString("tel"));
            map.put("province", set.getString("province"));
            map.put("city", set.getString("city"));
            map.put("district", set.getString("district"));
            map.put("address", set.getString("address"));
            map.put("date", set.getString("create_time"));
            list.add(map);

        }
        con.close();
        return list;
    }

}
